<template>
  <div class="datetime-nav">
    <el-date-picker
      v-model="startDateModal"
      :type="type"
      :placeholder="startPlaceholder"
      :value-format="valueFormat"
      default-time="00:00:00"
      @change="onChangeStart"
    />
    <p class="fault">-</p>
    <el-date-picker
      v-model="endDateModal"
      :type="type"
      :placeholder="endPlaceholder"
      :value-format="valueFormat"
      default-time="23:59:59"
      @change="onChangeEnd"
    />
  </div>
</template>

<script>
export default {
  name: 'YxhyDatetimeRange',
  props: {
    startDate: {
      type: String,
      default: '',
      required: true
    },
    endDate: {
      type: String,
      default: '',
      required: true
    },
    startPlaceholder: {
      type: String,
      default: '开始日期'
    },
    endPlaceholder: {
      type: String,
      default: '结束日期'
    },
    type: {
      type: String,
      default: 'datetime'
    }
  },
  data() {
    return {
      startDateModal: undefined,
      endDateModal: undefined
    }
  },
  computed: {
    valueFormat() {
      const { type } = this.$props
      if (type === 'datetime') return 'yyyy-MM-dd HH:mm:ss'
      else if (type === 'month') return 'yyyy-MM'
      else return 'yyyy-MM-dd HH:mm:ss'
    }
  },
  watch: {
    startDate: {
      handler(val) {
        this.startDateModal = val
      }
    },
    endDate: {
      handler(val) {
        this.endDateModal = val
      }
    }
  },
  methods: {
    onChangeStart(event) {
      const val = event || ''
      this.$emit('update:startDate', val)
    },
    onChangeEnd(event) {
      const val = event || ''
      this.$emit('update:endDate', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.datetime-nav {
  display: flex;
  align-items: center;
  .fault {
    color: #999;
    padding: 0 4px;
  }
}

::v-deep .el-input--medium .el-input__inner {
  text-align: center;
}
</style>
